#!/bin/bash
set -e

if ! hash helm 2>/dev/null; then
    exit 0
fi

cd $(dirname $0)/..
. ./scripts/version

rm -rf build/charts
mkdir -p build dist/artifacts
cp -rf charts build/
rm -rf build/charts/fleet-crd/templates/crds.yaml

sed -i \
    -e 's/^version:.*/version: '${HELM_VERSION}'/' \
    -e 's/appVersion:.*/appVersion: '${HELM_VERSION}'/' \
    build/charts/fleet/Chart.yaml

sed -i \
    -e 's/tag:.*/tag: '${HELM_TAG}'/' \
    build/charts/fleet/values.yaml

sed -i \
    -e 's/version:.*/version: '${HELM_VERSION}'/' \
    -e 's/appVersion:.*/appVersion: '${HELM_VERSION}'/' \
    build/charts/fleet-crd/Chart.yaml

sed -i \
    -e 's/version:.*/version: '${HELM_VERSION}'/' \
    -e 's/appVersion:.*/appVersion: '${HELM_VERSION}'/' \
    build/charts/fleet-agent/Chart.yaml

sed -i \
    -e 's/tag: dev/tag: '${HELM_TAG}'/' \
    build/charts/fleet-agent/values.yaml

go run ./cmd/codegen crds ./build/charts/fleet-crd/templates/crds.yaml
helm package -d ./dist/artifacts ./build/charts/fleet
helm package -d ./dist/artifacts ./build/charts/fleet-crd
helm package -d ./dist/artifacts ./build/charts/fleet-agent
